package com.project.yuepai.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.yuepai.dao.DynamicMapper;
import com.project.yuepai.dto.DynamicDto;
import com.project.yuepai.service.DynamicService;
import com.project.yuepai.util.Constant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DynamicServiceImpl implements DynamicService{

    @Resource
    private DynamicMapper dynamicMapper;

    @Override
    public PageInfo<DynamicDto> findAllDynamics(Integer page, Integer limit,String nickName, Integer userRole, String userPhone) {
        PageHelper.startPage(page,limit);
        List<DynamicDto> allDynamics = dynamicMapper.findAllDynamics(nickName, userRole, userPhone);
        for(int i=0;i<allDynamics.size();++i){
            List<String> image_set = new ArrayList<>();
            String[] paths = allDynamics.get(i).getImagePaths().split(";");
            for(String path:paths){
                path = Constant.IMG_PATH_PREFIX + path;
                image_set.add(path);
            }
            allDynamics.get(i).setImageSet(image_set);
        }
        return new PageInfo<DynamicDto>(allDynamics);
    }

    @Transactional
    @Override
    public boolean deleteDynamic(Integer dynamicId) {
        if(dynamicMapper.deleteDynamic(dynamicId)==1){
            if(dynamicMapper.deleteDynamicInRelation(dynamicId)==1)
                return true;
        }
        return false;
    }
}
